package org.puremvc.as3.multicore.utilities.unifiedloader.clients.flex
{

/*
 	PureMVC UnifiedLoader Utility
 	Copyright (c) 2008 neil manuell
  	Your reuse is governed by the Creative Commons Attribution 3.0 License
 */
	
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	
	import mx.events.ModuleEvent;
	import mx.modules.IModuleInfo;
	import mx.modules.ModuleManager;
	
	import org.puremvc.as3.multicore.utilities.unifiedloader.clients.LoaderClientBase;
	import org.puremvc.as3.multicore.utilities.unifiedloader.patterns.factories.ModuleFactory;
	import org.puremvc.as3.multicore.utilities.unifiedloader.interfaces.ILoaderClientInfo;
	


	
	public class FlexModuleClient extends LoaderClientBase
	{
		public static const NAME:String = "flexModuleClient";


		private var mModuleInfo:IModuleInfo
		
		public function FlexModuleClient(cli:ILoaderClientInfo)
		{
			super(cli)
		}

		
		override protected function initialiseLoad():void
		{
			mModuleInfo = ModuleManager.getModule(loaderClientInfo.urlRequest.url);    
		}
		
		override protected function startLoad():void
		{
			mModuleInfo.load();
		}
		
		override protected function addListeners():void{
		
			 mModuleInfo.addEventListener(ModuleEvent.READY, loadCompleteHandler); 
             mModuleInfo.addEventListener(ModuleEvent.ERROR, errorHandler); 
             mModuleInfo.addEventListener(ModuleEvent.PROGRESS, relayHandler); 
             mModuleInfo.addEventListener(ModuleEvent.SETUP, relayHandler);   
             mModuleInfo.addEventListener(ModuleEvent.UNLOAD, relayHandler);          
				
			
		}
		
		override protected function removeListeners():void{
			
			 mModuleInfo.removeEventListener(ModuleEvent.READY, loadCompleteHandler); 
             mModuleInfo.removeEventListener(ModuleEvent.ERROR, errorHandler); 
             mModuleInfo.removeEventListener(ModuleEvent.PROGRESS, relayHandler); 
             mModuleInfo.removeEventListener(ModuleEvent.SETUP, relayHandler);   
             mModuleInfo.removeEventListener(ModuleEvent.UNLOAD, relayHandler);        
					
		}
		
		override protected function handleError(event:Event):void
		{
			var e:IOErrorEvent;
			
			e = new IOErrorEvent(	IOErrorEvent.IO_ERROR, 
									false, 
									false,
									ModuleEvent(event).errorText	);
			relayHandler(e);
		}
		
		// NOTE for some reason these handlers are not fired in the flex debug mode,
		// unless you have break points inside them?? work fine otherwise.
		override protected  function prepareAsset(event:Event):void{
			

        	loaderClientInfo.setAsset( new ModuleFactory(mModuleInfo) );

			
			
		}
		
		

	
		

	
		
	}
}

